package com.one.consumer.reserve.ui.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.one.common.common.order.extra.ReserveExtra;
import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.manager.imagchoose.ChooseManager;
import com.one.common.manager.imagchoose.IChooseImg;
import com.one.common.model.extra.BaseExtra;
import com.one.common.model.extra.DefaultExtra;
import com.one.common.utils.ClickUtils;
import com.one.common.utils.StringUtils;
import com.one.common.utils.list.ListUtils;
import com.one.common.view.photolayout.SortableNinePhotoLayout;
import com.one.common.view.preview.PreviewPicActivity;
import com.one.common.view.widget.BigEditView;
import com.one.common.view.widget.HorizontalMenuItem;
import com.one.common.view.widget.MyTitleBar;
import com.one.consumer.BaseActivityC;
import com.one.consumer.R;
import com.one.consumer.main.ui.MainActivity;
import com.one.consumer.reserve.model.bean.TimeBean;
import com.one.consumer.reserve.model.extra.AreaExtra;
import com.one.consumer.reserve.presenter.AddServicePresenter;
import com.one.consumer.reserve.ui.view.AddServiceView;
import com.one.consumer.user.model.item.AddressItem;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/10/27 10:56 PM
 * description ：一键报修
 **/
@Route(path = RouterPath.ADD_SERVICE)
public class AddServiceActivity extends BaseActivityC<AddServicePresenter> implements AddServiceView, SortableNinePhotoLayout.Delegate {
    @BindView(R.id.hmi_3)
    HorizontalMenuItem hmi3;
    @BindView(R.id.tv_contact)
    TextView tvContact;
    @BindView(R.id.tv_contact_phone)
    TextView tvContactPhone;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.rl_add_address)
    RelativeLayout rlAddAddress;
    @BindView(R.id.tv_area_1)
    TextView tvArea1;
    @BindView(R.id.ll_add_area)
    LinearLayout llAddArea;
    @BindView(R.id.tv_area_2)
    TextView tvArea2;
    @BindView(R.id.tv_area_dsc)
    TextView tvAreaDsc;
    @BindView(R.id.ll_area)
    LinearLayout llArea;
    @BindView(R.id.snp_photo_service)
    SortableNinePhotoLayout snpPhotoService;
    @BindView(R.id.be_dsc)
    BigEditView beDsc;
    @BindView(R.id.hmi_time)
    HorizontalMenuItem hmiTime;
    @BindView(R.id.iv_icon)
    ImageView ivIcon;
    @BindView(R.id.tv_btn)
    TextView tvBtn;
    @BindView(R.id.ll_submit)
    LinearLayout llSubmit;
    @BindView(R.id.add_tips)
    TextView tipsTV;
    @BindView(R.id.btn_cankao)
    TextView btnCankao;

    public static final int RESULT_ADDRESS = 111;
    public static final int RESULT_AREA = 222;
    private OptionsPickerView pickerView;
    private ArrayList<TimeBean.TimeBeansBean> optionsItems = new ArrayList<>();
    /**
     * 选中的时间
     */
    private TimeBean.TimeBeansBean timeBeansBean;

    @Override
    protected void initPresenter() {
        mPresenter = new AddServicePresenter(this, this);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_add_service;
    }

    @Override
    protected void initTitle() {
        super.initTitle();
        getMyTitleBar().setMode(MyTitleBar.Mode.BACK_TITLE)
                .setTitleText("一键报修");
    }

    @Override
    public void initView() {
        super.initView();
        uploadLoading = new ProgressDialog(this);
        snpPhotoService.setDelegate(this);
        pickerView = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                hmiTime.setRightTxt(optionsItems.get(options1).getTime());
                timeBeansBean = optionsItems.get(options1);
            }
        }).setTitleSize(18)
                .build();

        pickerView.setPicker(optionsItems);
        if(!MainActivity.TIPS.isEmpty()){
            tipsTV.setText(MainActivity.TIPS);
        }else{
            tipsTV.setVisibility(View.GONE);
        }

    }

    @Override
    public void initData() {
        super.initData();
        mPresenter.getTimeList();
        mPresenter.getDefaultAddress();
    }

    /**
     * 添加地址
     */
    @OnClick({R.id.hmi_3, R.id.rl_add_address})
    public void addAddress() {
        RouterManager.getInstance().goForResult(this,
                RouterPath.SELECT_ADDRESS, RESULT_ADDRESS, new DefaultExtra("1"));
    }

    /**
     * 添加区域
     */
    @OnClick({R.id.ll_add_area, R.id.ll_area})
    public void addArea() {
        RouterManager.getInstance().goForResult(this, RouterPath.SELECT_AREA);
    }

    /**
     * 添加时间
     */
    @OnClick({R.id.hmi_time})
    public void addTime() {
        pickerView.show();
    }

    @OnClick({R.id.btn_cankao})
    public void cankao(){
//        startActivity();
        if(!MainActivity.CANKAO.isEmpty()){
            RouterManager.getInstance().go(RouterPath.TO_WEBVIEW,new DefaultExtra(MainActivity.CANKAO));
        }else{
            showToast("没有获取到页面地址");
        }

    }

    /**
     * 提交
     */
    @OnClick({R.id.ll_submit})
    public void submit() {
        if (ClickUtils.isFastClick()) return;
        mPresenter.submit(snpPhotoService.getData(), addressItem,
                beDsc.getText(), areaExtra, timeBeansBean);
    }

    @Override
    public void onClickAddNinePhotoItem(SortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, ArrayList<String> models) {
        ChooseManager.getChooser().chooseSingle(this);
    }

    @Override
    public void onClickDeleteNinePhotoItem(SortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, String model, ArrayList<String> models) {
        snpPhotoService.removeItem(position);
    }

    @Override
    public void onClickNinePhotoItem(SortableNinePhotoLayout sortableNinePhotoLayout, View view, int position, String model, ArrayList<String> models) {
        PreviewPicActivity.starPreview(this, sortableNinePhotoLayout.getData(), position);
    }

    @Override
    public void onNinePhotoItemExchanged(SortableNinePhotoLayout sortableNinePhotoLayout, int fromPosition, int toPosition, ArrayList<String> models) {

    }


    private AddressItem addressItem;
    private AreaExtra areaExtra;

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null) {
            if (resultCode == RESULT_OK) {
                // 选择图片的结果
                ArrayList<String> pathList =
                        data.getStringArrayListExtra(IChooseImg.EXTRA_RESULT_SELECTION);
                if (ListUtils.isNotEmpty(pathList)) {
                    snpPhotoService.addLastItem(pathList.get(0));
                }

            } else if (resultCode == RESULT_ADDRESS) {
                // 选择地址
                addressItem = (AddressItem) data.getSerializableExtra(BaseExtra.getExtraName());
                // 设置数据
                setAddressData(addressItem);
            } else if (resultCode == RESULT_AREA) {
                // 选择区域
                areaExtra = (AreaExtra) data.getSerializableExtra(AreaExtra.getExtraName());
                setAreaData(areaExtra);
            }
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void setTimeData(ArrayList<TimeBean.TimeBeansBean> data) {
        this.optionsItems.addAll(data);
    }

    /**
     * 设置地址信息
     */
    public void setAddressData(AddressItem item) {
        if (item != null && StringUtils.isNotBlank(item.getAddress()) &&
                StringUtils.isNotBlank(item.getAddress_name())) {
            addressItem = item;
            hmi3.setVisibility(View.GONE);
            rlAddAddress.setVisibility(View.VISIBLE);
            tvContact.setText(item.getAddress_name());
            tvContactPhone.setText(item.getAddress_mobile());
            tvAddress.setText(item.getAddress());
        }
    }

    /**
     * 设置区域信息
     */
    public void setAreaData(AreaExtra extra) {
        if (extra != null) {
            tvArea1.setText(extra.getNames().get(0));
            llArea.setVisibility(View.VISIBLE);
            tvArea2.setText(extra.getNames().get(1));
            if (extra.getNames().size() > 2) {
                tvAreaDsc.setVisibility(View.VISIBLE);
                tvAreaDsc.setText(extra.getNames().get(2));
            } else {
                tvAreaDsc.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 图片上传loading
     */
    public ProgressDialog uploadLoading;

    @Override
    public void startUpload() {
        uploadLoading.show();
        llSubmit.setEnabled(false);
//        tvBtn.setText("提交中...");
    }

    @Override
    public void finishUpload() {
        uploadLoading.dismiss();
        llSubmit.setEnabled(true);
//        tvBtn.setText("立即报修");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (uploadLoading != null)
            uploadLoading.dismiss();
    }
}
